package com.sws.person.ball;

import com.sws.device.domain.DevDevice;
import com.sws.device.mapper.DevDeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "camera.server")
public class CameraServerProperties {
    private String baseUrl;
    private String accessToken;
    private String photoPrefix; // 图片前缀路径
    
    @Autowired
    private DevDeviceMapper devDeviceMapper;

    // Getters and Setters
    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }
    
    /**
     * 根据设备SN获取设备基础URL
     * @param sn 设备序列号
     * @return 设备基础URL
     */
    public String getBaseUrlBySn(String sn) {
        if (sn == null || sn.isEmpty()) {
            return baseUrl; // 如果SN为空，返回默认URL
        }
        
        DevDevice device = devDeviceMapper.selectDevDeviceBySn(sn);
        if (device != null && device.getIp() != null && !device.getIp().isEmpty()) {
            return "http://" + device.getIp();
        }
        
        return baseUrl; // 如果没找到设备或IP为空，返回默认URL
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getPhotoPrefix() {
        return photoPrefix;
    }
    
    /**
     * 根据设备SN获取图片前缀URL
     * @param sn 设备序列号
     * @return 图片前缀URL
     */
    public String getPhotoPrefixBySn(String sn) {
        if (sn == null || sn.isEmpty()) {
            return photoPrefix; // 如果SN为空，返回默认前缀
        }
        
        DevDevice device = devDeviceMapper.selectDevDeviceBySn(sn);
        if (device != null && device.getIp() != null && !device.getIp().isEmpty()) {
            return "http://" + device.getIp() + "/";
        }
        
        return photoPrefix; // 如果没找到设备或IP为空，返回默认前缀
    }

    public void setPhotoPrefix(String photoPrefix) {
        this.photoPrefix = photoPrefix;
    }
}
